package com.zdb.java.thread;

import org.junit.Test;

import java.util.List;

public class ThreadLocalTest {

    @Test
    public void test() {
        ThreadLocal<List<String>> threadLocal = new ThreadLocal<>();
        threadLocal.get();
    }

    /**
     * 测试ThreadLocal中ThreadLocalMap中entry为WeakReference,发生GC时，会不会被回收
     *
     * 不会回收，因为ThreadLocalMap中 Entry[] table为强引用，只有在这个强引用中删除引用了，就会被回收，
     * 所以，尽量手动调用ThreadLocal的remove方法，来释放内存。
     * 
     */
    @Test
    public void testGC() {

        ThreadLocal<String> threadLocal = new ThreadLocal<>();

        threadLocal.set("aaaa");

        System.gc();

        String str = threadLocal.get();
        System.out.println(str);

    }
}
